package fun.boomcat.luckyhe.mirai.plugin;

import fun.boomcat.luckyhe.mirai.plugin.command.OpCommand;
import fun.boomcat.luckyhe.mirai.plugin.command.OpMcCommand;
import fun.boomcat.luckyhe.mirai.plugin.command.PlayerCommand;
import fun.boomcat.luckyhe.mirai.plugin.command.PlayerMcCommand;
import fun.boomcat.luckyhe.mirai.plugin.config.RconCommandConfigOperation;
import fun.boomcat.luckyhe.mirai.plugin.event.RconCommandMessageListenerHost;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.extension.PluginComponentStorage;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.console.permission.PermissionId;
import net.mamoe.mirai.console.permission.PermissionRegistryConflictException;
import net.mamoe.mirai.console.permission.PermissionService;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import net.mamoe.mirai.event.GlobalEventChannel;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class RconCommand extends JavaPlugin {

//    单例
    public static final RconCommand INSTANCE = new RconCommand();

    private Permission playerPerm;
    private Permission playerMcPerm;
    private Permission opPerm;
    private Permission opMcPerm;

    private RconCommand() {
        super(new JvmPluginDescriptionBuilder(
                "luckyhe.rconcommand",
                "1.0"
        ).build());
    }

    @Override
    public void onLoad(@NotNull PluginComponentStorage $this$onLoad) {
        try {
            RconCommandConfigOperation.initConfigPath(getConfigFolder(), getResource("config.properties", StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEnable() {
        INSTANCE.getLogger().info("开始加载插件");
        loadPermissions();
        INSTANCE.getLogger().info("注册权限完成");
        loadEvents();
        INSTANCE.getLogger().info("注册事件完成");
        loadCommands();
        INSTANCE.getLogger().info("注册指令完成");
    }

    public void loadEvents() {
        GlobalEventChannel.INSTANCE.registerListenerHost(new RconCommandMessageListenerHost());
    }

    public void loadCommands() {
        String commandPrefix = CommandManager.INSTANCE.getCommandPrefix();

        String[] playerCommandSecondaryNames = {};
        CommandManager.INSTANCE.registerCommand(new PlayerCommand(
                this,
                "mc",
                playerCommandSecondaryNames,
                commandPrefix + "mc <操作>",
                "mc相关指令",
                playerPerm,
                false
        ), false);

        String[] playerMcCommandSecondaryNames = {};
        CommandManager.INSTANCE.registerCommand(new PlayerMcCommand(
                this,
                "/",
                playerMcCommandSecondaryNames,
                commandPrefix + "/ <指令>",
                "用于向mc服务器发送自定义指令",
                playerMcPerm,
                false
        ), false);

        String[] opCommandSecondaryNames = {"管理mc"};
        CommandManager.INSTANCE.registerCommand(new OpCommand(
                this,
                "opmc",
                opCommandSecondaryNames,
                commandPrefix + "opmc <操作>",
                "管理mc相关指令",
                opPerm,
                false
                ), false);

        String[] opMcCommandSecondaryNames = {};
        CommandManager.INSTANCE.registerCommand(new OpMcCommand(
                this,
                "//",
                opMcCommandSecondaryNames,
                commandPrefix + "// <指令>",
                "向mc服务器发送op指令",
                opMcPerm,
                false
        ), false);
    }

    public void loadPermissions() {
        PermissionId playerPermId = new PermissionId("rconcommand", "command.mc");
        PermissionId playerMcPermId = new PermissionId("rconcommand", "command./");
        PermissionId opPermId = new PermissionId("rconcommand", "command.opmc");
        PermissionId opMcPermId = new PermissionId("rconcommand", "command.//");

        try {
            playerPerm = PermissionService.getInstance().register(
                    playerPermId,
                    "mc相关指令",
                    Permission.getRootPermission()
            );
            playerMcPerm = PermissionService.getInstance().register(
                    playerMcPermId,
                    "用于向mc服务器发送自定义指令",
                    Permission.getRootPermission()
            );
            opPerm = PermissionService.getInstance().register(
                    opPermId,
                    "mc的管理相关指令",
                    Permission.getRootPermission()
            );
            opMcPerm = PermissionService.getInstance().register(
                    opMcPermId,
                    "向mc服务器发送op指令",
                    Permission.getRootPermission()
            );
        } catch (PermissionRegistryConflictException e) {
            e.printStackTrace();
        }
    }
}
